/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package topico.ejb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import com.google.gson.Gson;
import java.util.Map;
import javax.ejb.EJB;
import logger.ejb.Logger4JLocal;
import negocio.EstructuraTopico;


/**
 *
 * @author Comun
 */



@Stateless
public class Topico implements TopicoLocal {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    private EstructuraTopico estructura = new EstructuraTopico();
    
    @EJB
    private Logger4JLocal log4j;
    
    private void readPropiedades() {
        // Obtengo una enumeracion de llaves, cada llave permitira
        // obtener un valor dentro del properties
        
       log4j.setLogger("Topico"); 
       
       estructura.Lista.clear();
        Properties p = new Properties();
        InputStream is;
        try {
            is = new  FileInputStream ("c:\\Topico.properties");
            p.load(is);
            is.close();
            String userName = p.getProperty("TOPICO");

            StringTokenizer tokens=new StringTokenizer(userName, "|");
            while(tokens.hasMoreTokens()){
                estructura.Lista.add(tokens.nextToken());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Topico.class.getName()).log(Level.SEVERE, null, ex);
            log4j.logError(ex.toString());
            
        }catch  (Exception ex1){
            Logger.getLogger(Topico.class.getName()).log(Level.SEVERE, null, ex1);
            log4j.logError(ex1.toString());
            
        } 
    }       
    
    @Override
    public int validarTopico(String topico) {
        readPropiedades();
        Iterator i = estructura.Lista.iterator();
        while(i.hasNext())
        {
            if(topico.equalsIgnoreCase(i.next().toString()))
            {
                return 0;
            }
        }
        return -1;            
    }
    

    @Override
    public String devolverListaTopicos() {
        readPropiedades();
        Gson retorno = new Gson();
        return retorno.toJson(estructura);
        
    }
}


